import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import {
  IsString,
  IsOptional,
  IsNumber,
  IsBoolean,
  IsObject,
  Min,
  Max,
  Length,
  IsEnum,
} from 'class-validator';
import { Transform } from 'class-transformer';

export class CreateRoomDto {
  @ApiPropertyOptional({
    description: '房间名称',
    example: '我的视频通话',
    maxLength: 100,
  })
  @IsOptional()
  @IsString()
  @Length(1, 100)
  name?: string;

  @ApiPropertyOptional({
    description: '房间描述',
    example: '这是一个私人视频通话房间',
    maxLength: 500,
  })
  @IsOptional()
  @IsString()
  @Length(1, 500)
  description?: string;

  @ApiPropertyOptional({
    description: '最大参与者数量',
    example: 4,
    minimum: 2,
    maximum: 10,
    default: 2,
  })
  @IsOptional()
  @IsNumber()
  @Min(2)
  @Max(10)
  @Transform(({ value }) => parseInt(value))
  maxParticipants?: number = 2;

  @ApiPropertyOptional({
    description: '房间密码',
    example: 'mypassword123',
    maxLength: 50,
  })
  @IsOptional()
  @IsString()
  @Length(4, 50)
  password?: string;

  @ApiPropertyOptional({
    description: '房间过期时间（分钟）',
    example: 60,
    minimum: 5,
    maximum: 1440,
    default: 60,
  })
  @IsOptional()
  @IsNumber()
  @Min(5)
  @Max(1440) // 最大24小时
  @Transform(({ value }) => parseInt(value))
  expiryMinutes?: number = 60;

  @ApiPropertyOptional({
    description: '房间设置',
    example: {
      audioEnabled: true,
      videoEnabled: true,
      screenShareEnabled: true,
      chatEnabled: true,
      recordingEnabled: false,
    },
  })
  @IsOptional()
  @IsObject()
  settings?: {
    audioEnabled?: boolean;
    videoEnabled?: boolean;
    screenShareEnabled?: boolean;
    chatEnabled?: boolean;
    recordingEnabled?: boolean;
  };

  @ApiPropertyOptional({
    description: '房间元数据',
    example: {
      userAgent: 'Mozilla/5.0...',
      browser: 'Chrome',
      platform: 'Windows',
    },
  })
  @IsOptional()
  @IsObject()
  metadata?: {
    userAgent?: string;
    browser?: string;
    platform?: string;
    [key: string]: any;
  };
}

export class JoinRoomDto {
  @ApiProperty({
    description: '房间ID',
    example: '123e4567-e89b-12d3-a456-426614174000',
  })
  @IsString()
  roomId: string;

  @ApiPropertyOptional({
    description: '房间密码（如果需要）',
    example: 'mypassword123',
  })
  @IsOptional()
  @IsString()
  password?: string;

  @ApiPropertyOptional({
    description: '用户昵称',
    example: '张三',
    maxLength: 50,
  })
  @IsOptional()
  @IsString()
  @Length(1, 50)
  nickname?: string;

  @ApiPropertyOptional({
    description: '用户头像URL',
    example: 'https://example.com/avatar.jpg',
  })
  @IsOptional()
  @IsString()
  avatar?: string;

  @ApiPropertyOptional({
    description: '媒体设备信息',
    example: {
      hasAudioInput: true,
      hasVideoInput: true,
      audioInputId: 'default',
      videoInputId: 'default',
    },
  })
  @IsOptional()
  @IsObject()
  mediaDevices?: {
    audioInputId?: string;
    videoInputId?: string;
    audioOutputId?: string;
    hasAudioInput?: boolean;
    hasVideoInput?: boolean;
  };

  @ApiPropertyOptional({
    description: '参与者元数据',
    example: {
      browser: 'Chrome',
      platform: 'Windows',
      deviceType: 'desktop',
    },
  })
  @IsOptional()
  @IsObject()
  metadata?: {
    browser?: string;
    platform?: string;
    deviceType?: 'desktop' | 'mobile' | 'tablet';
    timezone?: string;
    language?: string;
    [key: string]: any;
  };
}

export class UpdateRoomDto {
  @ApiPropertyOptional({
    description: '房间名称',
    example: '更新的房间名称',
    maxLength: 100,
  })
  @IsOptional()
  @IsString()
  @Length(1, 100)
  name?: string;

  @ApiPropertyOptional({
    description: '房间描述',
    example: '更新的房间描述',
    maxLength: 500,
  })
  @IsOptional()
  @IsString()
  @Length(1, 500)
  description?: string;

  @ApiPropertyOptional({
    description: '最大参与者数量',
    example: 6,
    minimum: 2,
    maximum: 10,
  })
  @IsOptional()
  @IsNumber()
  @Min(2)
  @Max(10)
  maxParticipants?: number;

  @ApiPropertyOptional({
    description: '房间状态',
    example: 'active',
    enum: ['waiting', 'active', 'ended'],
  })
  @IsOptional()
  @IsEnum(['waiting', 'active', 'ended'])
  status?: 'waiting' | 'active' | 'ended';

  @ApiPropertyOptional({
    description: '房间设置',
    example: {
      audioEnabled: true,
      videoEnabled: false,
      screenShareEnabled: true,
    },
  })
  @IsOptional()
  @IsObject()
  settings?: {
    audioEnabled?: boolean;
    videoEnabled?: boolean;
    screenShareEnabled?: boolean;
    chatEnabled?: boolean;
    recordingEnabled?: boolean;
  };
}

export class UpdateParticipantDto {
  @ApiPropertyOptional({
    description: '用户昵称',
    example: '新昵称',
    maxLength: 50,
  })
  @IsOptional()
  @IsString()
  @Length(1, 50)
  nickname?: string;

  @ApiPropertyOptional({
    description: '音频状态',
    example: true,
  })
  @IsOptional()
  @IsBoolean()
  audioEnabled?: boolean;

  @ApiPropertyOptional({
    description: '视频状态',
    example: false,
  })
  @IsOptional()
  @IsBoolean()
  videoEnabled?: boolean;

  @ApiPropertyOptional({
    description: '屏幕共享状态',
    example: true,
  })
  @IsOptional()
  @IsBoolean()
  screenShareEnabled?: boolean;

  @ApiPropertyOptional({
    description: '静音状态',
    example: false,
  })
  @IsOptional()
  @IsBoolean()
  isMuted?: boolean;

  @ApiPropertyOptional({
    description: '媒体设备信息',
  })
  @IsOptional()
  @IsObject()
  mediaDevices?: {
    audioInputId?: string;
    videoInputId?: string;
    audioOutputId?: string;
    hasAudioInput?: boolean;
    hasVideoInput?: boolean;
  };
}
